﻿using JinianNet.JNTemplate;

namespace RuoVea.CodeGen.Server.Util;

/// <summary>
/// 
/// </summary>
public class JNTemplateEx
{
    /// <summary>
    /// 
    /// </summary>
    /// <param name="name"></param>
    /// <param name="content"></param>
    /// <returns></returns>
    public static ITemplate CreateTemplate(string name,string content){
        Engine.Configure(c => {
            c.TagPrefix = "${{";
            c.TagSuffix = "}}";
            c.TagFlag = '$';
            c.EnableCache = false;// 确保缓存启用
        });
        name = name + IdGenerator.IdStr();
        // 加载模板
        var template = Engine.CreateTemplate(name,content);
        //template.Set("Now", DateTime.Now);
        //template.SetStaticType("DateTime", typeof(DateTime));
        template.SetStaticType(typeof(DateTime));
        template.SetStaticType("string", typeof(string));
        template.Set("FirstLower", new Func<string, string>(Extensions.FirstLower));
        template.Set("FirstUpper", new Func<string, string>(Extensions.FirstUpper));
        template.Set("ToCamel", new Func<string, string>(Extensions.ToCamel));
        template.Set("ToCamelAny", new Func<string, string>(Extensions.ToCamelAny));

        return template;
    }
}
